<?php
require_once "MyRabbitMq.php";

use MyRabbitMq\MyRabbitMq;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;

$exchangeName = 'exchange'; //交换机名

$queueName = 'requeue'; //队列名称

$routingKey = 'key'; //路由关键字

$rabbit = new MyRabbitMq();

$ch = $rabbit->getChannel();

$rabbit->setExchange($exchangeName, AMQPExchangeType::DIRECT, false, true, false);

$rabbit->setQueue($queueName, false, true, false, false);

$rabbit->bindQueueExchange($queueName, $exchangeName);

function callback(AMQPMessage $message)
{
    if ($message->getBody() != 1) {
        echo "消费{$message->getBody()}成功".PHP_EOL;
        $message->ack();
    } else {
        echo "重新返回队列".PHP_EOL;
        $message->nack(true); //消息为1时重新返回队列
    }
    usleep(1000);
}

$rabbit->consume($queueName, "callback");

while ($ch->is_consuming()) {
    $ch->wait();
}

$rabbit->close();